package com.drinks.yiniang.ui.fragment.user.wish;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.home.RecommendsGoodsAdapter;
import com.drinks.yiniang.common.recycler.decoration.HomeGoodsItemDecoration;
import com.drinks.yiniang.entity.goods.GoodsJson;
import com.drinks.yiniang.io.api.ShoppingCartApiIO;
import com.drinks.yiniang.io.api.WishApiIO;
import com.drinks.yiniang.ui.activity.goods.GoodsDetailsActivity;
import com.drinks.yiniang.ui.widget.recycler.BaseRecyclerView;
import com.drinks.yiniang.utils.RecyclerUtils;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.entity.BaseJson;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.ui.fragment.BaseLazyFragment;
import com.hansen.library.utils.CommonUtils;

import java.util.ArrayList;

/**
 * 商品心愿单
 *
 * @author han
 * @date 2019/09/08
 * A simple {@link Fragment} subclass.
 */
public class GoodsWishFragment extends BaseLazyFragment implements BaseQuickAdapter.OnItemClickListener,
        BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.RequestLoadMoreListener {

    private final int REQ_GOODS_DETAIL_CODE = 0x01;

    private BaseRecyclerView rcv_goods_wish_list;

    private RecommendsGoodsAdapter mGoodsAdapter;

    private int mPages;

    public GoodsWishFragment() {
        // Required empty public constructor
    }

    public static GoodsWishFragment newInstance() {
        return new GoodsWishFragment();
    }


    @Override
    protected int getResID() {
        return R.layout.fragment_goods_wish;
    }

    @Override
    protected void initView(View mView) {
        rcv_goods_wish_list = mView.findViewById(R.id.rcv_goods_wish_list);
    }

    @Override
    protected void initData() {
        mContext = getActivity();

        getActivity();

        initAdapter();
    }

    private void initAdapter() {
        rcv_goods_wish_list.setLayoutManager(RecyclerUtils.getVerticalGridLayout(mContext, 2));
        rcv_goods_wish_list.addItemDecoration(HomeGoodsItemDecoration.getItemDPDecoration(10));
        rcv_goods_wish_list.setHasFixedSize(true);

        mGoodsAdapter = new RecommendsGoodsAdapter(mContext, new ArrayList<GoodsJson.GoodsList>());
        mGoodsAdapter.setCommonEmpty(getLayoutInflater(), rcv_goods_wish_list);
        mGoodsAdapter.bindToRecyclerView(rcv_goods_wish_list);
    }

    @Override
    protected void listener() {
        mGoodsAdapter.setOnItemClickListener(this);
        mGoodsAdapter.setOnItemChildClickListener(this);
        mGoodsAdapter.setOnLoadMoreListener(this, rcv_goods_wish_list);
    }

    @Override
    protected void widgetClick(View v) {

    }

    @Override
    protected void lazyLoadData() {
        getWishList();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == Constants.RESULT_CODE_NORMAL && requestCode == REQ_GOODS_DETAIL_CODE) {

        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        GoodsJson.GoodsList data = mGoodsAdapter.getItem(position);

        if(data == null) {
            return;
        }

        Intent intent = new Intent(mContext, GoodsDetailsActivity.class);
        intent.putExtra(Constants.KeyGoodsId, data.getId());
        startActivityForResult(intent, REQ_GOODS_DETAIL_CODE);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        GoodsJson.GoodsList data = mGoodsAdapter.getItem(position);

        if(data != null && view.getId() == R.id.iv_home_goods_add_cart) addToCart(data.getId());
    }

    private void addToCart(String goodsId) {
        showLoadingDialog();

        ShoppingCartApiIO.getInstance().addToShopCart(goodsId, new APIRequestCallback<BaseJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(BaseJson res) {
                ToastUtils.showShort(getString(R.string.text_add_success));
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    @Override
    public void onLoadMoreRequested() {
        WishApiIO.getInstance().getWishList(Constants.TYPE_WISH_LIST_GOODS, mPages,
                new APIRequestCallback<GoodsJson, Tuple2<Integer, String>>() {
                    @Override
                    public void onSuccess(GoodsJson res) {
                        if (mGoodsAdapter == null) {
                            return;
                        }

                        mPages++;

                        mGoodsAdapter.addData(res.getData().getData());
                        if (CommonUtils.isHasMoreData(res.getData().getData())) {
                            mGoodsAdapter.loadMoreComplete();
                        } else {
                            mGoodsAdapter.loadMoreEnd();
                        }
                    }

                    @Override
                    public void onError(Tuple2<Integer, String> error) {
                        ToastUtils.showShort(error._2);
                        mGoodsAdapter.loadMoreFail();
                    }

                    @Override
                    public void onComplete() {
                        dismissDialog();
                    }
                });
    }

    private void getWishList() {
        showLoadingDialog();

        mPages = 1;

        WishApiIO.getInstance().getWishList(Constants.TYPE_WISH_LIST_GOODS, mPages,
                new APIRequestCallback<GoodsJson, Tuple2<Integer, String>>() {
                    @Override
                    public void onSuccess(GoodsJson res) {
                        if (mGoodsAdapter == null) {
                            return;
                        }

                        mPages++;

                        mGoodsAdapter.getData().clear();
                        mGoodsAdapter.addData(res.getData().getData());

                        if (CommonUtils.isHasMoreData(res.getData().getData())) {
                            mGoodsAdapter.loadMoreComplete();
                        } else {
                            mGoodsAdapter.loadMoreEnd();
                        }
                    }

                    @Override
                    public void onError(Tuple2<Integer, String> error) {
                        ToastUtils.showShort(error._2);
                        mGoodsAdapter.loadMoreFail();
                    }

                    @Override
                    public void onComplete() {
                        dismissDialog();
                    }
                });
    }
}
